package com.example.demo.service.Impl;

import com.example.demo.ao.XinAddAo;
import com.example.demo.mapper.XinAddMapper;
import com.example.demo.service.XinAddService;
import com.example.demo.vo.XinAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class XinAddServiceImpl implements XinAddService {

    @Autowired
    private XinAddMapper xinAddMapper;

    
    // 定义日期格式
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");

    public XinAddAo processTimeRange(XinAddAo timeAo) {
        // 解析输入日期
        LocalDate startDate = LocalDate.parse(timeAo.getStartTime(), DATE_FORMATTER);
        LocalDate endDate = LocalDate.parse(timeAo.getEndTime(), DATE_FORMATTER);
        LocalDate currentDate = LocalDate.now();

        // 1. 判断endTime是否是当前月
        boolean isEndCurrentMonth = isSameMonth(endDate, currentDate);

        // 2. 判断是否同月
        boolean isSameMonth = isSameMonth(startDate, endDate);

        // 根据条件分支设置字段
        if (isSameMonth) {
            handleSameMonthCase(timeAo, startDate, endDate, isEndCurrentMonth);
        } else {
            handleDifferentMonthCase(timeAo, startDate, endDate, isEndCurrentMonth);
        }

        return timeAo;
    }

    // 判断两个日期是否在同一个月
    private boolean isSameMonth(LocalDate date1, LocalDate date2) {
        return date1.getYear() == date2.getYear()
                && date1.getMonth() == date2.getMonth();
    }

    // 处理同月的情况
    private void handleSameMonthCase(XinAddAo timeAo, LocalDate startDate, LocalDate endDate, boolean isEndCurrentMonth) {
        if (isEndCurrentMonth) {
            // 情况1：同月且是当前月
            timeAo.setStartTimeDay(startDate.format(DATE_FORMATTER));
            timeAo.setEndTimeDay(endDate.format(DATE_FORMATTER));

            YearMonth startYearMonth = YearMonth.of(startDate.getYear(), 1);
            YearMonth endYearMonth = YearMonth.from(endDate).minusMonths(1);

            timeAo.setStartTimeYear(startYearMonth.format(MONTH_FORMATTER));
            timeAo.setEndTimeYear(endYearMonth.format(MONTH_FORMATTER));
        } else {
            // 情况3：同月但不在当前月
            timeAo.setStartTimeMonth(YearMonth.from(startDate).format(MONTH_FORMATTER));
            timeAo.setEndTimeMonth(YearMonth.from(endDate).format(MONTH_FORMATTER));

            timeAo.setStartTimeYear(YearMonth.of(startDate.getYear(), 1).format(MONTH_FORMATTER));
            timeAo.setEndTimeYear(timeAo.getEndTimeMonth()); // 根据需求修正
        }
    }

    // 处理不同月的情况
    private void handleDifferentMonthCase(XinAddAo timeAo, LocalDate startDate, LocalDate endDate, boolean isEndCurrentMonth) {
        if (isEndCurrentMonth) {
            // 情况2：不同月且end是当前月
            timeAo.setStartTimeDay(endDate.withDayOfMonth(1).format(DATE_FORMATTER));
            timeAo.setEndTimeDay(endDate.format(DATE_FORMATTER));

            timeAo.setStartTimeMonth(YearMonth.from(startDate).format(MONTH_FORMATTER));
            timeAo.setEndTimeMonth(YearMonth.from(endDate).minusMonths(1).format(MONTH_FORMATTER));

            timeAo.setStartTimeYear(YearMonth.of(startDate.getYear(), 1).format(MONTH_FORMATTER));
            timeAo.setEndTimeYear(timeAo.getEndTimeMonth());
        } else {
            // 情况4：不同月且end不在当前月
            timeAo.setStartTimeMonth(YearMonth.from(startDate).format(MONTH_FORMATTER));
            timeAo.setEndTimeMonth(YearMonth.from(endDate).format(MONTH_FORMATTER));

            timeAo.setStartTimeYear(YearMonth.of(startDate.getYear(), 1).format(MONTH_FORMATTER));
            timeAo.setEndTimeYear(timeAo.getEndTimeMonth());
        }
    }

    @Override
    public List<XinAddVo> getXinAddList(XinAddAo xinAddAo) {
        XinAddAo xinAddAo1 = processTimeRange(xinAddAo);

        return List.of();
    }
}
